<template>
  <!--招采管理-班组招采列表-查看会签表弹窗和表单-->
  <j-modal
    title="信息"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    okText="平台班组招采询价">
    <a-spin :spinning="confirmLoading">
      <a-button type="primary" @click="exportExcel" icon="download" style="margin-bottom: 20px;">下载</a-button>
      <!-- <div class="no-print" style="text-align: right">
          <a-button v-print="'#printContent'" ghost type="primary">打印</a-button>
        </div> -->
      <section class="abcdefg" id="aTable" v-if="supplierDataList.length > 0">
        <table border="1" height="40px" bordercolor="#D8D8D8">
          <tr>
            <!-- <td><img src="./1.png" alt="" /></td> -->
            <td align="center" width="1051px">询价表</td>
          </tr>
        </table>
        <a-table :pagination="false"  size="middle" bordered :columns="columns" :dataSource="countersignDataList" :loading="loading" style="margin-bottom: 20px;">
        </a-table>
        <!-- <table border="1" height="200px" bordercolor="#D8D8D8">
          <tr>
            <td align="center" width="600px" >利润分析</td>
            <td colspan="3">{{this.profitabilityAnalysis}}</td>
          </tr>
          <tr>
            <td align="center" width="600px" >备注</td>
            <td colspan="3">{{this.remark}}</td>
          </tr>
          <tr>
            <td align="center" width="600px">项目采购意见</td>
            <td width="600px"></td>
            <td align="center" width="600px">采购部经理意见</td>
            <td width="600px"></td>
          </tr>
          <tr>
            <td align="center" width="600px">成本控制中心经理意见</td>
            <td width="600px"></td>
            <td align="center" width="600px">财务经理意见</td>
            <td width="600px"></td>
          </tr>
          <tr>
            <td align="center" width="600px">项目主管意见</td>
            <td width="600px"></td>
            <td align="center" width="600px">分管领导意见</td>
            <td width="600px"></td>
          </tr>
          <tr>
            <td align="center" width="600px">总经理批准</td>
            <td width="600px"></td>
            <td align="center" width="600px">副董事长批准</td>
            <td width="600px"></td>
          </tr>
          <tr>
            <td align="center" width="600px">董事长批准</td>
            <td width="600px" colspan="3"></td>
          </tr>
        </table> -->
      </section>
    </a-spin>

  </j-modal>
</template>

<script>
import ATextarea from 'ant-design-vue/es/input/TextArea'
import ExcelJS from 'exceljs'
import { saveAs } from 'file-saver'
import { httpAction } from '@api/manage'

//  import { mixinDevice } from '@/utils/mixin'
// import { JeecgListMixin } from '@/mixins/JeecgListMixin'
//  item.supplierName, supplierPriceItem.publicBiddingName
const renderContent = (value, row, index) => {
  const obj = {
    children: value,
    attrs: {},
  }

  // console.log(value)
  // console.log(row)
  // console.log(index)

  if (index === 1) {
    obj.attrs.rowSpan = 2
  }

  // These two are merged into above cell
  if (index === 2) {
    obj.attrs.rowSpan = 0
  }

  return obj
}

import Bus from './bus'
export default {
  name: 'huiqian',
  // mixins: [JeecgListMixin, mixinDevice],
  props: {
    value:Boolean,
    publicBiddingId: {
      type: String,
      default: '',
    },
    biddingSuppliersIds: {
      type: String,
      default: '',
    },
    // supplierDataList: {
    //   type: Array,
    //   default() {
    //     return []
    //   },
    // },
    supplierData: {
      type: Object,
      default() {
        return {}
      },
    },

  },
  components: {
    ATextarea,
  },
  data() {
    return {
      supplierDataList:[],
      NameProject:[],
      title: '发标',
      id: '',
      width: 1100,
      visible: false,
      disableSubmit: true,
      confirmLoading: false,
      profitabilityAnalysis:'',
      remark:'',
      loading: false,
      columns: [
        {
          title: '序号',
          align: 'center',
          // dataIndex: 'rowIndex',
          key:'rowIndex',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
          // customRender: renderContent,
          // customRender(_, row) {
          //   return {
          //     children: row.id,
          //     attrs: {
          //       rowSpan: row.idRowSpan,
          //     },
          //   }
          // },
        },
        {
          title: '项目名称',
          align: 'center',
          dataIndex: 'name',
          customRender(_, row, index) {
            return row.supplierPriceList[index].name
            // return {
            //   children: row.supplierPriceList[0].name,
            //   attrs: {
            //     rowSpan: row.supplierNameRowSpan,
            //   },
            // }
          },
        },
        // {
        //   title: '物资名称',
        //   align: 'center',
        //   dataIndex: 'supplierPriceItem.publicBiddingName',
        //   customRender(_, row) {
        //     return {
        //       children: row.supplierPriceList[0].publicBiddingName,
        //       attrs: {
        //         rowSpan: row.supplierPriceList[0].publicBiddingName,
        //       },
        //     }
        //   },
        // },
        {
          title: '计量单位',
          align: 'center',
          dataIndex: 'supplierPriceItem.unit',
          customRender(_, row, index) {
            return row.supplierPriceList[index].materialsUnit
            return {
              children: row.supplierPriceList[0].materialsUnit,
              attrs: {
                rowSpan: row.supplierPriceList[0].materialsUnit,
              },
            }
          },
        },
        {
          title: '工程量(暂定)',
          align: 'center',
          dataIndex: 'supplierPriceItem.num',
          customRender(_, row, index) {
            return row.supplierPriceList[index].num
          },
        },
        {
          title: '含税中标价',
          align: 'center',
          dataIndex: 'supplierPriceItem.num',
          width:100,
          children: [
            {
              title: '综合单价',
              customRender(_, row,index) {
                return row.supplierPriceList[index].price

          },
              // width: 90,
            },
            {
              title: '合价',
              customRender(_, row, index) {
                return row.supplierPriceList[index].money

          },
              // width: 90,
            },
          ],
        },
        // {
        //   title: '毛盛跃  13860102988',
        //   align: 'center',
        //   dataIndex: 'supplierPriceItem.num',
        //   width:100,
        //   children: [
        //     {
        //       title: '不含增值税综合单价',

        //       // width: 90,
        //     },
        //     {
        //       title: '合价',

        //       // width: 90,
        //     },
        //   ],
        // },
        // {
        //   title: '毛盛跃  13860102988',
        //   align: 'center',
        //   dataIndex: 'supplierPriceItem.num',
        //   width:100,
        //   children: [
        //     {
        //       title: '不含增值税综合单价',

        //       // width: 90,
        //     },
        //     {
        //       title: '合价',

        //       // width: 90,
        //     },
        //   ],
        // },
        // {
        //   title: '毛盛跃  13860102988',
        //   align: 'center',
        //   dataIndex: 'supplierPriceItem.num',
        //   width:100,
        //   children: [
        //     {
        //       title: '不含增值税综合单价',

        //       // width: 90,
        //     },
        //     {
        //       title: '合价',

        //       // width: 90,
        //     },
        //   ],
        // },
        // {
        //   title: '毛盛跃  13860102988',
        //   align: 'center',
        //   dataIndex: 'supplierPriceItem.num',
        //   width:100,
        //   children: [
        //     {
        //       title: '不含增值税综合单价',

        //       // width: 90,
        //     },
        //     {
        //       title: '合价',

        //       // width: 90,
        //     },
        //   ],
        // },
        // {
        //   title: '毛盛跃  13860102988',
        //   align: 'center',
        //   dataIndex: 'supplierPriceItem.num',
        //   width:100,
        //   children: [
        //     {
        //       title: '不含增值税综合单价',

        //       // width: 90,
        //     },
        //     {
        //       title: '合价',

        //       // width: 90,
        //     },
        //   ],
        // },
        // {
        //   title: '毛盛跃  13860102988',
        //   align: 'center',
        //   dataIndex: 'supplierPriceItem.num',
        //   width:100,
        //   children: [
        //     {
        //       title: '不含增值税综合单价',

        //       // width: 90,
        //     },
        //     {
        //       title: '合价',

        //       // width: 90,
        //     },
        //   ],
        // },
      ],
      countersignData: {},
      // url: {
      //   list: '/bidding/wtBiddingSupplier/list',
      // }
    }
  },

  created() {
    this.remark=this.remark
    // console.log('446',this.remark);
  },
  computed: {
    countersignDataList() {
      let list = []

      if (this.supplierDataList.length > 0) {
        this.supplierDataList.forEach((item, itemIndex) => {
          item.supplierPriceList.forEach((supplierPriceItem, supplierPriceItemIndex) => {
            list.push({
              ...item,
              supplierPriceItem: supplierPriceItem,
            })
          })
        })

        const rowSpan = (key) => {
          let arr = list
            .reduce((result, item) => {
              if (result.indexOf(item[key]) < 0) {
                result.push(item[key])
              }
              return result
            }, [])
            .reduce((result, keys) => {
              const children = list.filter((item) => item[key] === keys)
              result = result.concat(
                children.map((item, index) => ({
                  ...item,
                  [`${key}RowSpan`]: index === 0 ? children.length : 0,
                }))
              )
              return result
            }, [])
          list = arr
        }
        rowSpan('id')
        // rowSpan('supplierName')
      }
      // console.log('3432', { list })

      return list
    },
  },
  methods: {
    handleOk(){
      let type = '平台班组招采询价';
      this.visible = false;
      let countersignDataList = JSON.stringify(this.countersignDataList)
      this.$emit('tolink',type,countersignDataList);
    },
    showInfo(record){

      this.remark=record.remark
      this.profitabilityAnalysis=record.profitabilityAnalysis
      this.visible = true
      this.confirmLoading = true

      httpAction('/bidding/wtPublicBidding/getCountersignData?publicBiddingId=' + record.id,{},'GET').then(res => {
        this.supplierDataList = res.result.list
        this.confirmLoading = false
        // console.log('2',res.result);
       this.NameProject=res.result
      })
    },
    add() {
      this.visible = true
    },
    edit() {
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.visible = false
    },
    exportExcel() {
      const workbook = new ExcelJS.Workbook()
      workbook.creator = 'Me'
      workbook.lastModifiedBy = 'Her'
      workbook.created = new Date(1985, 8, 30)
      workbook.modified = new Date()
      workbook.lastPrinted = new Date(2016, 9, 27)
      // 创建一个第一行和列冻结的工作表
      const sheet = workbook.addWorksheet('My Sheet', { views: [{ xSplit: 1, ySplit: 1 }] })

      // sheet.columns = [
      //   { header: '序号', key: 'id', width: 10 },
      //   { header: '供应商/联系人/联系电话、传真、手机', key: 'supplierName', width: 50, },
      //   { header: '物资名称', key: 'publicBiddingName', width: 10}
      // ];

      sheet.addRow([])
      // sheet.getRow(1).height = 35

      sheet.addRow([this.NameProject.project + '' + this.NameProject.name + '询价表'])
      sheet.mergeCells(`A2:L2`)
      sheet.getCell(`A2:L2`).alignment = { vertical: 'middle', horizontal: 'center' }
      sheet.getCell(`A2:L2`).font = { size: 16, bold: true }
      sheet.addRow(['','','','','含税中标价'])

      sheet.addRow([
        '序号',
        '工程名称',
        '计量单位',
        '工程量',
        '综合单价',
        "合价"
        // '金额(不含税)',
        // '税率',
        // '金额(含税)',
        // '付款方式',
        // '备注',
      ])
      sheet.mergeCells(`E3:F3`)
      sheet.mergeCells(`A3:A4`)
      sheet.getCell('A3').value='序号'
      sheet.mergeCells(`B3:B4`)
      sheet.getCell('B3').value='项目名称'
      sheet.mergeCells(`C3:C4`)
      sheet.getCell('C3').value='计量单位'
      sheet.mergeCells(`D3:D4`)
      sheet.getCell('D3').value='工程量'
      let rowNum = 4

      this.countersignDataList.forEach((item, itemIndex) => {
        rowNum++
        // console.log({item});
        sheet.addRow([
          itemIndex,
            // item.name,
            item.supplierPriceItem.name,
            item.supplierPriceItem.materialsUnit,
            item.supplierPriceItem.num,

            item.supplierPriceItem.price,
            item.supplierPriceItem.money,
            // supplierPriceItem.taxTate,
            // supplierPriceItem.taxMoney,
            // item.paymentMethod,
            // item.remark,
          ])
          // console.log('21',rowNum);
          sheet.getRow(`${rowNum}`).alignment = {
            vertical: 'middle',
            horizontal: 'center',
            wrapText: true,
          }
      })
      const alignment = { vertical: 'middle', horizontal: 'center', wrapText: true }
      rowNum++
      // sheet.addRow(['利润分析'])
      sheet.getCell(`A${rowNum }`).value = '利润分析'
      sheet.mergeCells(`A${rowNum }:B${rowNum }`)
      sheet.getCell(`A${rowNum }:B${rowNum }`).alignment = alignment
      sheet.mergeCells(`C${rowNum }:L${rowNum }`)
      sheet.getCell(`C${rowNum }:L${rowNum }`).alignment = alignment
      sheet.getCell(`C${rowNum }:L${rowNum }`).value = this.profitabilityAnalysis
      rowNum++
      sheet.getCell(`A${rowNum }`).value = '备注'
      sheet.mergeCells(`A${rowNum }:B${rowNum }`)
      sheet.getCell(`A${rowNum }:B${rowNum }`).alignment = alignment
      sheet.mergeCells(`C${rowNum }:L${rowNum }`)
      sheet.getCell(`C${rowNum }:L${rowNum }`).alignment = alignment
      sheet.getCell(`C${rowNum }:L${rowNum }`).value = this.remark
      rowNum++
      sheet.getCell(`A${rowNum }`).value = '项目采购意见'
      sheet.mergeCells(`A${rowNum }:B${rowNum }`)
      sheet.getCell(`A${rowNum }:B${rowNum }`).alignment = alignment
      sheet.mergeCells(`C${rowNum }:F${rowNum }`)
      sheet.getCell(`C${rowNum }:F${rowNum }`).alignment = alignment
      sheet.getCell(`G${rowNum }`).value = '采购部经理意见'
      sheet.mergeCells(`G${rowNum }:H${rowNum }`)
      sheet.getCell(`G${rowNum }:H${rowNum }`).alignment = alignment
      sheet.mergeCells(`I${rowNum }:L${rowNum }`)
      sheet.getCell(`I${rowNum }:L${rowNum }`).alignment = alignment
      rowNum++
      sheet.getCell(`A${rowNum }`).value = '成本控制中心经理意见'
      sheet.mergeCells(`A${rowNum }:B${rowNum }`)
      sheet.getCell(`A${rowNum }:B${rowNum }`).alignment = alignment
      sheet.mergeCells(`C${rowNum }:F${rowNum }`)
      sheet.getCell(`C${rowNum }:F${rowNum }`).alignment = alignment
      sheet.getCell(`G${rowNum }`).value = '财务经理意见'
      sheet.mergeCells(`G${rowNum }:H${rowNum }`)
      sheet.getCell(`G${rowNum }:H${rowNum }`).alignment = alignment
      sheet.mergeCells(`I${rowNum }:L${rowNum }`)
      sheet.getCell(`I${rowNum }:L${rowNum }`).alignment = alignment
      rowNum++
      sheet.getCell(`A${rowNum }`).value = '项目主管意见'
      sheet.mergeCells(`A${rowNum }:B${rowNum }`)
      sheet.getCell(`A${rowNum }:B${rowNum }`).alignment = alignment
      sheet.mergeCells(`C${rowNum }:F${rowNum }`)
      sheet.getCell(`C${rowNum }:F${rowNum }`).alignment = alignment
      sheet.getCell(`G${rowNum }`).value = '分管领导意见'
      sheet.mergeCells(`G${rowNum }:H${rowNum }`)
      sheet.getCell(`G${rowNum }:H${rowNum }`).alignment = alignment
      sheet.mergeCells(`I${rowNum }:L${rowNum }`)
      sheet.getCell(`I${rowNum }:L${rowNum }`).alignment = alignment
      rowNum++
      sheet.getCell(`A${rowNum }`).value = '总经理批准'
      sheet.mergeCells(`A${rowNum }:B${rowNum }`)
      sheet.getCell(`A${rowNum }:B${rowNum }`).alignment = alignment
      sheet.mergeCells(`C${rowNum }:F${rowNum }`)
      sheet.getCell(`C${rowNum }:F${rowNum }`).alignment = alignment
      sheet.getCell(`G${rowNum }`).value = '副董事长批准'
      sheet.mergeCells(`G${rowNum }:H${rowNum }`)
      sheet.getCell(`G${rowNum }:H${rowNum }`).alignment = alignment
      sheet.mergeCells(`I${rowNum }:L${rowNum }`)
      sheet.getCell(`I${rowNum }:L${rowNum }`).alignment = alignment
      rowNum++
      sheet.getCell(`A${rowNum }`).value = '董事长批准'
      sheet.mergeCells(`A${rowNum }:B${rowNum }`)
      sheet.getCell(`A${rowNum }:B${rowNum }`).alignment = alignment
      sheet.mergeCells(`C${rowNum }:F${rowNum }`)
      sheet.getCell(`C${rowNum }:F${rowNum }`).alignment = alignment


      workbook.xlsx.writeBuffer().then((data) => {
        // console.log('32435',this.NameProject);
        const blob = new Blob([data], { type: '' })
        saveAs(blob, this.NameProject.project + '' + this.NameProject.name + '询价表' + '.xlsx')
      })

    },

    /* handleOk() {
      this.visible = true
      this.id = this.publicBiddingId
      this.confirmLoading = true
      httpAction(
        '/bidding/wtPublicBidding/updateBidding',
        {
          id: this.id,
          biddingSuppliers: this.biddingSuppliersIds,
          biddingSupplierStatus: '2',
          status: '3',
        },
        'POST'
      )
        .then((res) => {
          // this.countersignData = res.result
        })
        .finally(() => {
          this.confirmLoading = false
          this.close()
          this.$emit('handleOk')
        })

      // console.log(record.id)
    }, */
  },
}
</script>
<style scoped>
.div1 {
  width: 100%;
  text-align: center;
  font-size: 16px;
}
</style>